const clipboardWatcher = require('./clipborad-watcher');
require("./clipboard-op");

module.exports = () => {
  const { status } = global.clipboard_data.get();

  return {
    async onReady(ctx) {
      const {clipboard, mainWindow, ipcMain} = ctx;
      const watcher = clipboardWatcher(clipboard, {
        // (optional) delay in ms between polls
        watchDelay: 1000,
        // handler for when image data is copied into the clipboard
        // onImageChange: function (nativeImage) {
        //   const target = {
        //     type: "img",
        //     content: nativeImage.toDataURL(),
        //   }
        //   records.push(target);
        //   global.clipboard_data.set(records);
        //   const view = mainWindow.getBrowserView();
        //   view.webContents.executeJavaScript(`window.saveClipboard(${JSON.stringify({
        //     type: "img",
        //     content: nativeImage.toDataURL(),
        //   })})`);
        // },
        // handler for when text data is copied into the clipboard
        onTextChange: function (text) {
          const { records } = global.clipboard_data.get();

          const target = {
            type: "text",
            content: text,
          }
          records.unshift(target);
          global.clipboard_data.set({records});
          const view = mainWindow.getBrowserView();
          // 打开了webContents
          if (view) {
            view.webContents.executeJavaScript(`window.updateRender()`);
          }
        }
      });
      if (status) {
        watcher.start();
      }
      ipcMain.on("clipboard-status-change", (e, arg) => {
        if (arg) {
          // todo
          watcher.start();
        } else {
          // todo
          watcher.stop();
        }
      });
    }
  }
}
